﻿//创建时间: 2010-11-18
//创建作者: 李晓忠
//功能说明: CMS系统 图片上传 
//修改: 2010-11-18
//修改: 2010-11-19 修改上传判断，添加结点目录规则
using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TRPOP.Common;
using TRPOP.CMS.Web.UI;

using TRPOP.CMS.Business;
using TRPOP.CMS.Model;
using Newtonsoft.Json;
using System.Text;
using System.Data;

namespace TRPOP.CMS.Web.Admin
{
    public partial class admin_img_picker : BaseAdmin
    {
        #region 页面变量
        protected int m_intNodeId = 0;
        protected string m_strType = "";
        protected string m_strAction = "";
        #endregion

        #region 页面事件
        protected void Page_Load(object sender, EventArgs e)
        {
            m_intNodeId = RequestHelper.RequestInt("nodeid");
            m_strType = RequestHelper.RequestString("type");
            m_strAction = RequestHelper.RequestString("ac").ToLower();

            if (!IsPostBack)
            {
                LoadBase();
                BindData();
            }
        }


        #endregion

        #region 页面方法

        public void LoadBase()
        {

            if (m_strAction == "upload")
            {
                HttpFileCollection MyFileColl = HttpContext.Current.Request.Files;
                if (MyFileColl.Count > 0)
                {
                    Upload();
                }
                else
                {
                    ExecuteJs("parent.alert(\"请选择要上传的图片\");");
                }

            }

        }


        public void BindData()
        {


        }

        private void AjaxMethod()
        {

        }

        private void Upload()
        {
            string strSavePath = "";
            string strWaterPic = "";
            ContentUpFileManage m_ContentUpFileManage = new ContentUpFileManage();
            ContentUpFileInfo m_ContentUpFileInfo = new ContentUpFileInfo();

            m_ContentUpFileInfo.AddTime = DateTime.Now;
            m_ContentUpFileInfo.AddUserId = mUserBaseInfo.UserBaseId;
            m_ContentUpFileInfo.AttributeID = (int)AttributeType.EnumElement.Pic;


            if (m_intNodeId > 0)
            {
                #region
                NodeInfo m_NodeInfo = (new NodeManage()).GetInfoById(m_intNodeId);

                if (m_NodeInfo != null)
                {
                    strWaterPic = m_NodeInfo.WaterPic;

                    m_ContentUpFileInfo.NodeID = m_intNodeId;


                    if (m_NodeInfo.SourcePSN != "" && m_NodeInfo.SourceUrl != "")
                    {

                        strSavePath = (new PSNManage()).GetPSNPathByPSN(m_NodeInfo.SourcePSN) + "/" + (new NodeManage()).RootReg(m_NodeInfo.SourceRootReg, m_ContentUpFileInfo.AddTime);
                        strSavePath = strSavePath.Replace("//", "/");
                    }
                    else if (SYSConfig.SystemVar_DefaultResourcePSN != "")
                    {

                        strSavePath = (new PSNManage()).GetPSNPathByPSN(SYSConfig.SystemVar_DefaultResourcePSN) + "/" + (new NodeManage()).RootReg(m_NodeInfo.SourceRootReg, m_ContentUpFileInfo.AddTime);
                        strSavePath = strSavePath.Replace("//", "/");
                    }
                    else
                    {
                        ExecuteJsWidthEnd("parent.alert('结点没有设置资源发布点')");
                    }

                }
                else
                {
                    //结点信息不存在
                    ExecuteJsWidthEnd("parent.alert('结点信息不存在')");
                }
                #endregion
            }
            //这里不对图片进行加水印的工作
            AttachmentInfo atcInfo = CMSImageHelper.CMSImageUpLoad(SYSConfig.AttachmentSetting.UpImageSize * 1024, strSavePath, strWaterPic, SYSConfig.AttachmentSetting.UpImageType, CMSImageHelper.WaterPicLocation.BottomRight);
            if (atcInfo.ErrorDescription == "")
            {

                #region 附件保存
                m_ContentUpFileInfo.FileRoot = atcInfo.FileName;
                m_ContentUpFileInfo.FileSize = (atcInfo.FileSize / 1024);
                m_ContentUpFileInfo.Category = atcInfo.FileType;
                m_ContentUpFileManage.Save(m_ContentUpFileInfo);
                #endregion

                ExecuteJsWidthEnd("parent.returnImg('" + atcInfo.FileName + "')");
            }
            else
            {
                ExecuteJsWidthEnd("parent.alert('" + atcInfo.ErrorDescription + "');parent.loadBarUnDisplay();");
            }

        }
        #endregion
    }
}
